package com.linkallcloud.web.face.base;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.linkallcloud.core.domain.Domain;
import com.linkallcloud.core.domain.FlowStep;
import com.linkallcloud.core.dto.CheckResult;
import com.linkallcloud.core.dto.Trace;
import com.linkallcloud.core.exception.BizException;
import com.linkallcloud.core.exception.Exceptions;
import com.linkallcloud.core.face.message.request.FlowStepFaceRequest;
import com.linkallcloud.core.face.message.request.ObjectFaceRequest;
import com.linkallcloud.core.face.message.response.ErrorFaceResponse;
import com.linkallcloud.core.lang.Strings;
import com.linkallcloud.core.manager.IFlowStepManager;
import com.linkallcloud.core.util.Utils;
import com.linkallcloud.core.www.ISimpleUser;
import com.linkallcloud.web.face.annotation.Face;
import com.linkallcloud.web.session.SessionBUser;
import com.linkallcloud.web.utils.Controllers;

public abstract class BaseFlowStepFace<T extends FlowStep, S extends IFlowStepManager<T>> extends BaseFace<T, S> {

    public BaseFlowStepFace() {
        super();
    }

    @Face(simple = true)
    @RequestMapping(value = "/finds", method = RequestMethod.POST)
    public @ResponseBody Object finds(FlowStepFaceRequest faceReq, CheckResult cr, Trace t, ISimpleUser su) {
        Utils.assertNotNull(faceReq.getObjId(), "参数objId不能为空");
        Utils.assertNotBlank(faceReq.getObjUuid(), "参数objUuid不能为空");
        Utils.assertNotBlank(faceReq.getObjType(), "参数objType不能为空");
        return manager().findByObj(t, faceReq.getFlowType(), faceReq.getObjId(), faceReq.getObjUuid(),
                faceReq.getObjType(), faceReq.getObjPropType());
    }

    @Face(simple = true)
    @RequestMapping(value = "/flow", method = RequestMethod.POST)
    public @ResponseBody Object flow(ObjectFaceRequest<T> fr, CheckResult cr, Trace t, HttpServletRequest request,
            ISimpleUser user) {
        if (!Controllers.checkReferer(request, true)) {
            return new ErrorFaceResponse(Exceptions.CODE_ERROR_AUTH_PERMISSION, "Referer验证未通过");
        }
        Utils.assertNotNull(fr.getData(), "BaseFlowStepFace:flow:fr.data不能为空");
        T entity = fr.getData();
        if (entity.isCompleteThenDealBz() == null) {
            entity.setCompleteThenDealBz(true);
        }
        if (user instanceof SessionBUser) {
            SessionBUser su = (SessionBUser) user;
            entity.setCuserId(su.id());
            entity.setCuserName(Strings.isBlank(entity.getCuserName()) ? su.name() : entity.getCuserName());
            entity.setCuserPhone(Strings.isBlank(entity.getCuserPhone()) ? su.getPhone() : entity.getCuserPhone());
        }

        return manager().flow(t, fr.getData());
    }

    @Override
    protected void doSave(Trace t, T entity, ISimpleUser su, CheckResult arg3) {
        Utils.assertNotNull(entity, "BaseFlowStepFace:doSave:entity不能为空");
        if (su instanceof SessionBUser) {
            entity.setCuserId(((SessionBUser) su).id());
        }
        super.doSave(t, entity, su, arg3);
    }

    @Face(simple = true)
    @RequestMapping(value = "/getObjFields", method = RequestMethod.POST)
    public @ResponseBody Object filterObjFields(FlowStepFaceRequest fr, CheckResult cr, Trace t,
            HttpServletRequest request, ISimpleUser su) {
        Domain entity = getBusinessEntity(t, fr, su);
        if (entity != null) {
            return entity.filterFields(fr.getObjPropType());
        }
        throw new BizException("e.param", "参数错误！！！");
    }

    protected abstract Domain getBusinessEntity(Trace t, FlowStepFaceRequest fr, ISimpleUser su);

}
